package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 聊天室客户端：将客户端说和听的过程定义在了线程里，并在start方法里启用线程
 * @author XingHaiYang
 * @date 2022/9/15 14:43
 */
public class Client3 {
    /**
     * 定义s对象
     */
    private Socket s;

    /**
     * 客户端的无参构造器，用以给s赋值，作用是在网络中通过ip找到远程计算机，并通过端口号找到该计算机上的服务
     */
    public Client3() {
        try {
            System.out.println("正在与远端计算机连接...");
            s = new Socket("localhost",8088);
            System.out.println("连接成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 自定义的start方法，用以封装自定义的线程
     */
    public void start(){
        //创建线程，用以读取服务端发过来的数据
        Runnable r = new ServerHandler();
        Thread t = new Thread(r);
        t.setDaemon(true);//设置守护线程
        t.start();
        //为客户端在主动说
        /*try {
            OutputStream out = s.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw);
            Scanner scan = new Scanner(System.in);
            while(true){
                String line = scan.nextLine();
                if("exit".equals(line)){
                    break;
                }
                pw.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }

    /**
     * 程序的入口，用以连接服务端，并且启动自定义的start方法与远端计算机进行交互
     * @param args
     */
    public static void main(String[] args) {
        Client3 c3 = new Client3();
        c3.start();
    }

    /**
     * 自定义线程类(内部类)，用以执行客户端与服务端的交互任务
     */
    private class ServerHandler implements Runnable{
        /**
         * 重写Runnable接口中的run方法，客户端在听
         */
        @Override
        public void run() {
            try {
                //客户端在听
                InputStream in = s.getInputStream();
                InputStreamReader isr = new InputStreamReader(in);
                BufferedReader br = new BufferedReader(isr);
                String listen;
                while ((listen = br.readLine()) != null){
                    System.out.println("服务端说："+listen);
                }
                //客户端在说
                OutputStream out = s.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                PrintWriter pw = new PrintWriter(bw);
                Scanner scan = new Scanner(System.in);
                while(true){
                    String say = scan.nextLine();
                    if("exit".equals(say)){
                        break;
                    }
                    pw.println(say);
                    System.out.println("客户端说："+say);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
